package palmerpuric.tweets2bits;

import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;

class PicBit extends Sprite {
	public static var firstDeath:Bool = false;
	// linija, od kjer naprej bodo cevi oz, kjer PicBit zgine
	private static var _hline:Int = Shredder.SUCKINGMACHINERY_BACKGROUND_OFFSET_TOP;
	
    private var _vx:Float;
    private var _vy:Float;
    private var _gravity:Float;
	private var _restitution:Float;
	private var _bounceFlag:Bool;
	private var _pipeStrength:Float;
	
	private var _starty:Float;
	private var _startx:Float;
	
	public function new(x : Float, y : Float,  mass : Float, pipeStr:Float, bitmap:BitmapData) : Void {
		super();
		initFizifi(x, y, mass, pipeStr);
		
		graphics.beginBitmapFill(bitmap);
		graphics.drawRect(x, y, bitmap.width, bitmap.height);
		graphics.endFill();
		
		addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}
	
	function initFizifi(x : Float, y : Float,  mass : Float, pipeStr:Float) {
		_starty = y;
		_startx = x;
		_vx = 0;
		_vy = 0;
		_gravity = 0.06;
		_restitution = 0.5 + mass;
		_bounceFlag = false;
		_pipeStrength = pipeStr;
	}
	
	function onEnterFrame(event:Event) : Void {
		// trampolincek na liniji - prvic odbije, drugic posucka
		if (!_bounceFlag) {
			if (hitTestPoint(_startx, _hline - 2)){
				_vy *= -_restitution;
				_bounceFlag = true;
			}
		}
		else{
			_vy += _pipeStrength;
			if (y > _hline - _starty - 7) {
				firstDeath = true;
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				Lib.current.removeChild(this);
			}
		}
		_vy += _gravity;
		y += _vy;
    }
}
